//
//  RecommendViewController.m
//  喜马拉雅改良版框架
//
//  Created by liuxin on 15/12/22.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "RecommendViewController.h"

@interface RecommendViewController ()<UITableViewDelegate, UITableViewDataSource, SDCycleScrollViewDelegate,LikeViewDelegate>

@property (nonatomic, strong)NSMutableArray *dataArray;

@property (nonatomic, strong)UITableView *myTableView;

@property (nonatomic, strong)NSMutableArray *roolArray; // 轮播图数组
@property (nonatomic, strong)NSMutableArray *roolImageArr;// 轮播图
@property (nonatomic, strong)SDCycleScrollView *myScrollView;

@property (nonatomic, strong)UIView *headerView;
@property(nonatomic,strong)NSMutableArray *allArr;
@end

@implementation RecommendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.allArr = [NSMutableArray array];
    self.view.backgroundColor = [UIColor colorWithWhite:0.882 alpha:1.000];
    _dataArray = [NSMutableArray array];
    
    _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStyleGrouped];
    //    _myTableView.backgroundColor = [UIColor redColor];
    // 用grouped 会多一块,所以加footer 设置成0.01
    _myTableView.sectionFooterHeight = 0.01;
    _myTableView.dataSource = self;
    _myTableView.delegate = self;
    [self.view addSubview:_myTableView];
    
    _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    _headerView.backgroundColor = [UIColor colorWithRed:0.929 green:1.000 blue:0.957 alpha:1.000];
    
    _myTableView.tableHeaderView = _headerView;
    
    // tableView的位置
    [_myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.and.left.right.mas_equalTo(0);
        
        make.bottom.mas_equalTo(0);
        
    }];
    
    // 注册cell
    [_myTableView registerClass:[KBLikeTableViewCell class] forCellReuseIdentifier:@"LikeTableViewCell"];
    [_myTableView registerClass:[KBFindTableViewCell class] forCellReuseIdentifier:@"FindTableViewCell"];
    [_myTableView registerClass:[KBListenTableViewCell class] forCellReuseIdentifier:@"ListenTableViewCell"];
    
    
    [self getData];
    
}

#pragma mark - 获取数据
- (void)getData
{
    NSString *url = @"http://mobile.ximalaya.com/mobile/discovery/v1/recommends?channel=ios-b1&device=iPhone&includeActivity=true&includeSpecial=true&scale=2&version=4.3.8";
    [AFNetworkTool getUrl:url body:self result:HXJSON headerFile:nil success:^(id result) {
        
        //初始化数组
        //        _hotRecommendsArray = [NSMutableArray array]; // 区数组
        _roolArray = [NSMutableArray array]; // 轮播图数据
        _roolImageArr = [NSMutableArray array]; // 轮播图
        
        NSMutableDictionary *bigDic = result;
        
        // 小编推荐
        KBLikeModel *editor = [[KBLikeModel alloc] init];
        [editor setValuesForKeysWithDictionary:bigDic[@"editorRecommendAlbums"]];
        [self.allArr addObject:editor];
        
        // 弹幕热聊
        KBLikeModel *bulletModel = [[KBLikeModel alloc] init];
        [bulletModel setValuesForKeysWithDictionary:bigDic[@"bulletArea"]];
        [self.allArr addObject:bulletModel];
        
        // 精品听单
        KBLikeModel *hearModel = [[KBLikeModel alloc] init];
        [hearModel setValuesForKeysWithDictionary:bigDic[@"specialColumn"]];
        [self.allArr addObject:hearModel];
        
        // 发现新奇
        KBLikeModel *likeModel = [[KBLikeModel alloc] init];
        [likeModel setValuesForKeysWithDictionary:bigDic[@"discoveryColumns"]];
        [self.allArr addObject:likeModel];
        
        // 热门推荐
        NSMutableDictionary *hotRecommends = bigDic[@"hotRecommends"];
        NSDictionary *list = [hotRecommends objectForKey:@"list"];
        for (NSDictionary *dic in list)
        {
            KBLikeModel *recommend = [[KBLikeModel alloc] init];
            [recommend setValuesForKeysWithDictionary:dic];
            [self.allArr addObject:recommend];
        }
        
        //        KBLikeModel *hotModel = [[KBLikeModel alloc] init];
        //        [hotModel setValuesForKeysWithDictionary:bigDic[@"hotRecommends"]];
        //        [self.allArr addObject:hotModel];
        //        NSLog(@"aaa%@", hotModel);
        
        // 轮播图数据和图片
        NSMutableDictionary *focusImages = [bigDic objectForKey:@"focusImages"];
        
        NSMutableArray *focusList = [focusImages objectForKey:@"list"];
        for (NSDictionary *dic in focusList)
        {
            KBRollModel *rollModel = [[KBRollModel alloc] init];
            [_roolArray addObject:rollModel];
            NSString *roll = [dic objectForKey:@"pic"];
            [_roolImageArr addObject:roll];
        }
        
        
        // 轮播图
        _myScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200) imageURLStringsGroup:_roolImageArr];
        
        _myScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
        _myScrollView.delegate = self;
        _myScrollView.dotColor = [UIColor colorWithRed:0.823 green:0.886 blue:1.000 alpha:1.000];
        [_headerView addSubview:_myScrollView];
        
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            
        });
        // 清除缓存
        [_myScrollView clearCache];
        [self.myTableView reloadData];
        
    } failure:^(NSError *error) {
        
        NSLog(@"请求数据失败");
    }];
    
}

#pragma mark 设置tableview区数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _allArr.count;
}

#pragma mark 设置tableview行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    KBLikeModel *model = [_allArr objectAtIndex:section];
    if ([model.title isEqualToString:@"精品听单"])
    {
        return model.list.count;
    }
    if ([model.title isEqualToString:@"发现新奇"]) {
        return model.list.count;
    }
    return 1;
}
#pragma mark - tableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *likeIdentifier = @"LikeTableViewCell";
    KBLikeTableViewCell *likeCell = [tableView dequeueReusableCellWithIdentifier:likeIdentifier];
    likeCell.likeDelegate = self; // 签代理
    
    
    KBLikeModel *model = [_allArr objectAtIndex:indexPath.section];
    NSDictionary *dic = [model.list objectAtIndex:indexPath.row];
    if ([model.title isEqualToString:@"小编推荐"])
    {
        likeCell.cellModel = model;
        likeCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return likeCell;
    }
    if ([model.title isEqualToString:@"发现新奇"])
    {
        static NSString *findIdentifier = @"FindTableViewCell";
        KBFindTableViewCell *findCell = [tableView dequeueReusableCellWithIdentifier:findIdentifier];
        // 显示cell右侧的箭头
        [findCell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
        
        findCell.dataDic = dic;
        //        findCell.cellModel = model;
        findCell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return findCell;
    }
    if ([model.title isEqualToString:@"精品听单"])
    {
        static NSString *listenIdentifier = @"ListenTableViewCell";
        KBListenTableViewCell *listenCell = [tableView dequeueReusableCellWithIdentifier:listenIdentifier];
        // 显示cell右侧的箭头
        [listenCell     setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
        listenCell.selectionStyle = UITableViewCellSelectionStyleNone;
        listenCell.cellModel = model;
        listenCell.dataDic = dic;
        return listenCell;
    }
    likeCell.cellModel = model;
    return likeCell;
}
#pragma mark - 设置区头View
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    KBLikeModel *model = [_allArr objectAtIndex:section];
    KBSectionHeaderView *headView = [[KBSectionHeaderView alloc] initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH, SCREEN_WIDTH*0.08)];
    headView.titleLabel.text = model.title;
    
    if (![model.title isEqualToString:@"发现新奇"])
    {
        headView.moreButton.tag = section;
        [headView.moreButton addTarget:self action:@selector(moreButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    else
    {
        [headView.moreButton setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
        [headView.moreButton setImage:[UIImage imageNamed:@""] forState:UIControlStateHighlighted];
    }
    
    
    return headView;
}
#pragma mark - 高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return SCREEN_WIDTH*0.08;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    KBLikeModel *model = [_allArr objectAtIndex:indexPath.section];
    if ([model.title isEqualToString:@"精品听单"]) {
        return 110;
    }
    if ([model.title isEqualToString:@"发现新奇"]) {
        return 90;
    }
    
    return (SCREEN_WIDTH - 60)/3+80;
}

#pragma mark 更多按钮方法
-(void)moreButtonAction:(UIButton *)btn
{
//        [self.headerView.timer setFireDate: [NSDate distantFuture]];
    
    if (btn.tag == 0 || btn.tag == 1 || btn.tag == 2) {
        
        if (btn.tag == 0)
        {
            
            KBMoreListViewController *moreListVC = [[KBMoreListViewController alloc] init];
            moreListVC.myTitle = @"小编推荐";
            moreListVC.moreUrl = @"http://mobile.ximalaya.com/mobile/discovery/v1/recommend/editor?device=iphone&pageId=1&pageSize=20&title=更多";
            NSLog(@"000");
            [(KBTabBarController *)self.tabBarController isHiddenTabBar:YES withViewController:moreListVC];
            [self.navigationController pushViewController:moreListVC animated:YES];
            
        }
        if (btn.tag == 1)
        {
            
            KBMoreHeatViewController *moreHeatVC = [[KBMoreHeatViewController alloc] init];
            moreHeatVC.myTitle = @"弹幕热聊";
//            moreHeatVC.moreUrl = @"http://mobile.ximalaya.com/mobile/discovery/v1/recommend/bulletArea?device=android&pageId=1&pageSize=30";
            moreHeatVC.tabBarController.tabBar.hidden = YES;
            NSLog(@"111");
            [(KBTabBarController *)self.tabBarController isHiddenTabBar:YES withViewController:moreHeatVC];
            [self.navigationController pushViewController:moreHeatVC animated:YES];
        }
        if (btn.tag == 2)
        {
            KBMoreListenViewController *moreListenVC = [[KBMoreListenViewController alloc] init];
            moreListenVC.myTitle = @"精品听单";
            [(KBTabBarController *)self.tabBarController isHiddenTabBar:YES withViewController:moreListenVC];
            NSLog(@"222");
            [self.navigationController pushViewController:moreListenVC animated:YES];
        }
    }
        else if (btn.tag != 2)
        {
            LBCCategoryListViewController *homeVC = [[LBCCategoryListViewController alloc] init];
            // tag值对应的section
            KBLikeModel *model = [_allArr objectAtIndex:btn.tag];
            NSLog(@"mo %ld", model.categoryId);
            homeVC.category.categoryId = [NSString stringWithFormat:@"%ld",model.categoryId];
            [(KBTabBarController *)self.tabBarController isHiddenTabBar:YES withViewController:homeVC];
            homeVC.category = model;
            homeVC.categoryId = [NSString stringWithFormat:@"%ld", model.categoryId];
            homeVC.contentType = [NSString stringWithFormat:@"%@", model.contentType];
            [self.navigationController pushViewController:homeVC animated:YES];
        }
}
#pragma mark - 实现代理方法 跳转到专辑详情
- (void)LikeViewdelegate:(NSIndexPath *)indexPath arr:(NSMutableArray *)arr
{
    
    KBAlbumsDetailViewController *albumsDetailVC = [[KBAlbumsDetailViewController alloc] init];
    
    
    NSDictionary *dic = [arr objectAtIndex:indexPath.row];
    albumsDetailVC.albumId = dic[@"albumId"];
    
    [(KBTabBarController *)self.tabBarController isHiddenTabBar:YES withViewController:albumsDetailVC];
    
    [self.navigationController pushViewController:albumsDetailVC animated:YES];
    
}


#pragma mark - tableView点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    KBLikeModel *model = [_allArr objectAtIndex:indexPath.section];
    NSDictionary *dic = [model.list objectAtIndex:indexPath.row];
    
    if ([model.title isEqualToString:@"精品听单"])
    {
        
        KBMusicListViewController *musicVC = [[KBMusicListViewController alloc] init];
        musicVC.specialId = dic[@"specialId"];
        musicVC.contentType = dic[@"contentType"];
        musicVC.title = @"精品听单";
        [(KBTabBarController *)self.tabBarController isHiddenTabBar:YES withViewController:musicVC];
        [self.navigationController pushViewController:musicVC animated:YES];
    }
    if ([model.title isEqualToString:@"发现新奇"])
    {
        KBShopViewController *shopVC = [[KBShopViewController alloc]init];
        
        if (indexPath.row == 2)
        {
            shopVC.urlStr = @"http://www.wemart.cn";
            shopVC.row = indexPath.row;
            [(KBTabBarController *)self.tabBarController isHiddenTabBar:YES withViewController:shopVC];
             shopVC.title = dic[@"title"];
            [self.navigationController pushViewController:shopVC animated:YES];
        }
        if (indexPath.row == 3)
        {
            shopVC.urlStr = @"http://m.diyring.cc/friend/d450780628b741f1";
            shopVC.row = indexPath.row;
            [(KBTabBarController *)self.tabBarController isHiddenTabBar:YES withViewController:shopVC];
            [self.navigationController pushViewController:shopVC animated:YES];
        }
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
